package com.example.login.vm

import androidx.lifecycle.viewModelScope
import com.example.common.vm.BaseViewModel
import com.example.login.intent.SimpleViewIntent
import com.example.login.model.SimpleRepository
import com.example.login.state.SimpleUiState
import com.tencent.mmkv.MMKV
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SimpleViewModel @Inject constructor(private val repository: SimpleRepository):BaseViewModel<SimpleViewIntent>(){
   private val _uiState = MutableStateFlow<SimpleUiState>(SimpleUiState.NoneState)
    val uiState:StateFlow<SimpleUiState> = _uiState.asStateFlow()
   override fun dispatchIntent(intent: SimpleViewIntent) {
            when(intent){
                is SimpleViewIntent.TypeToServer -> typeToServer()
                is SimpleViewIntent.RecommendToServer->recommendToServer(intent.page,intent.pageSize)
                is SimpleViewIntent.VideoToServer-> videoToServer(intent.id,intent.page,intent.size)
            }
    }

    private fun recommendToServer(page: Int, pageSize: Int) {
        viewModelScope.launch(Dispatchers.IO){
            try {
                val (code,data,msg) = repository.recommendToServer(page, pageSize)
                if(code == 0 && data.isNotEmpty()){
                    _uiState.value = SimpleUiState.RecommendSuccess(data)
                }else{
                    _uiState.value = SimpleUiState.RecommendFailure(msg)
                }
            }catch (e:Exception){
                e.printStackTrace()
                _uiState.value = SimpleUiState.VideoFailure(e.message?:"接口异常")
            }finally {
                cancel()
            }
        }
    }

    private fun typeToServer() {
        viewModelScope.launch(Dispatchers.IO){
            try {
                var resultData = repository.typeToServer()
                if(resultData.code == 0){
                    _uiState.value = SimpleUiState.TypeSuccess(resultData.data)
                }else{
                    _uiState.value = SimpleUiState.TypeFailure(resultData.msg)
                }
            }catch (e:Exception){
                e.printStackTrace()
                _uiState.value = SimpleUiState.TypeFailure(e.message?:"接口异常")
            }finally {
                cancel()
            }
        }
    }

    private fun videoToServer(id: String, page:Int,size:Int) {
        viewModelScope.launch(Dispatchers.IO){
            try {
                repository.videoToServer(id, page, size).collect{list->
                    if(list.isNotEmpty()){
                        _uiState.value = SimpleUiState.VideoSuccess(list)
                    }else{
                        _uiState.value = SimpleUiState.VideoFailure("暂无数据")
                    }
                }
            }catch (e:Exception){
                e.printStackTrace()
                _uiState.value = SimpleUiState.VideoFailure(e.message?:"接口异常")
            }finally {
                cancel()
            }
        }
    }
}